package net.jxtz.jxtzos.controller.auth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.entity.auth.WechatUser;
import net.jxtz.jxtzos.entity.web.R;
import net.jxtz.jxtzos.service.auth.WechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author a123
 */

@RestController
@CrossOrigin
@RequestMapping("/wechat_user")
@Slf4j
public class WechatUserController {

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public R add(@RequestBody WechatUser wechatUser){
        boolean save = wechatUserService.save(wechatUser);
        return save ? R.ok():R.error();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public R delete(@PathVariable Long id){
        boolean b = wechatUserService.removeById(id);
        return b ? R.ok():R.error();
    }

    @RequestMapping(value = "/list", method = RequestMethod.DELETE)
    public R list(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        Page<WechatUser> page = wechatUserService.page(new Page<>(pageNum, pageSize));
        List<WechatUser> records = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("total", total).data("list", records);
    }

    @RequestMapping(value = "/getWechatInfoByUserId", method = RequestMethod.GET)
    public R getWechatInfoByUserId(@RequestParam("user_id") Long id){
        List<WechatUser> list = wechatUserService.list(new QueryWrapper<WechatUser>().eq("user_id", id));
        return R.ok().data("list", list);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public R update(@RequestBody WechatUser wechatUser){
        boolean b = wechatUserService.updateById(wechatUser);
        return b ? R.ok():R.error();
    }

    private WechatUserService  wechatUserService;

    @Autowired
    public void setWechatUserService(WechatUserService wechatUserService){
        this.wechatUserService = wechatUserService;
    }

}
